package com.cosina.mr;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.cosina.mr.dao.DAOFactory;
import com.cosina.mr.model.MoneyEvent;

public class RecordListActivity extends Activity{
	private Cursor cursor;
	private ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.records);
        listView = (ListView)findViewById(R.id.list);
        
        cursor = DAOFactory.getMRDAO(this).findAll();
        
        SimpleCursorAdapter ca = new SimpleCursorAdapter(
        		this,
        		R.layout.listcolumn,
        		cursor,MoneyEvent.getListViewColumnNames(),
        		new int[]{	R.id.list_column_id, 
        					R.id.list_column_ocurrtime,
        					R.id.list_column_name,
        					R.id.list_column_money, 
        					R.id.list_column_desc});
        
        listView.setAdapter(ca);
        listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(RecordListActivity.this, EditRecordActivity.class);
				intent.putExtra("_id", id);
				startActivity(intent);
			}});
    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater flater = getMenuInflater();
		flater.inflate(R.menu.recordlistoptionsmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.ADD_RECORD:
				Intent goToAdd = new Intent(this,EditRecordActivity.class);
				startActivity(goToAdd);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}
    
	@Override
	protected void onResume() {
		super.onResume();
		cursor.requery();
	}
}